package com.nuanshui.heatedloan.task.failover.strategy;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import com.nuanshui.heatedloan.task.failover.TaskFailMessage;

public abstract class AbstractTaskFailStrategy implements TaskFailStrategy {
	
	private Logger logger = LoggerFactory.getLogger("task_fail");
	
	public final static HashMap<String,AtomicInteger> failNums = new HashMap<>();//执行错误的次数
	@Override
	public void handler(TaskFailMessage taskFailMessage) {
		 //处理错误次数
		  String code = taskFailMessage.getCode();
		  AtomicInteger num =   failNums.get(code);
		  int retryNum = 0;
		  if(num == null) {
			  num = new AtomicInteger(0);
			  failNums.put(code,num);
		  }else {
			  retryNum = num.incrementAndGet();
		  }
		  taskFailMessage.setNum(retryNum);
		  //向日志文件中记录任务执行失败的错误信息
		  MDC.put("unique",taskFailMessage.getCode());
		  String jobGroup = taskFailMessage.getJobGroup();
		  String jobName = taskFailMessage.getJobName();
		  StringBuffer sf = new StringBuffer(jobGroup);
		  sf.append(jobName).append(" 执行失败");
		  doHandler(taskFailMessage);		
	}
	
	public abstract void doHandler(TaskFailMessage taskFailMessage);

}
